package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;


    // 客户列表：老大可见
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo) {

        //客户信息的可见：管理员和市场经理才可见全部信息，专员只可见自己的
        Subject subject = SecurityUtils.getSubject();
        //获取当前登录用户的身份信息id
        Employee employee = (Employee) subject.getPrincipal();
        //可见条件内查询所有客户
        if (employee.isAdmin() || subject.hasRole("Market_Manager")) {
            //根据角色的sn来查询具有某角色的员工 r.sn IN('Market_Manager','Market')
            List<Employee> employees = customerService.selectByRoleSn("Market_Manager", "Market");
            model.addAttribute("employees", employees);

            //调用service的方法得到结果集
            PageInfo pageInfo = customerService.query(qo);
            //共享结果集：在页面遍历显示
            model.addAttribute("result", pageInfo);
        }
        return "customer/list"; //返回视图名称：加上前缀和后缀的ftl
    }
}
